#include "BankSubject.h"
#include "BankObserver.h"
#include "Account.h"



void BankSubject::attach(BankObserver* observer)
{
	m_observer_list.push_back(observer);
}

void BankSubject::detach(BankObserver* observer)
{
	m_observer_list.remove(observer);	
}

void BankSubject::notifyInvestStock()
{
	time_t now = time(0);
	tm* currentTime = gmtime(&now);
	std::list<BankObserver*>::iterator iter = m_observer_list.begin();
	
	while (iter != m_observer_list.end()){
		if ( (*iter)->getType() == BankObserver::stock && 			
			((Account*) *iter)->getSavingPeriod() == 10 &&			
			currentTime->tm_year -((Account*) *iter)->getOpenDate().tm_year < 5){
				(*iter)->update(BankObserver::investingEvent);
		}
		iter++;
	}
}

void BankSubject::notifyGiveBonus()
{
	std::list<BankObserver*>::iterator iter = m_observer_list.begin();
	while (iter != m_observer_list.end()){
		if ((*iter)->getType() == BankObserver::family)
		{
			(*iter)->update(BankObserver::depositEvent);
		}
		iter++;
	}
}

void BankSubject::notifyMeetingClerk()
{
	std::list<BankObserver*>::iterator iter = m_observer_list.begin();
	while (iter != m_observer_list.end()){
		if ((*iter)->getType() == BankObserver::twoYear){
			(*iter)->update(BankObserver::meetEvent);
		}
		iter++;
	}
}
